package com.yitiao.socket.chatroom.demo;

import com.yitiao.socket.demo.Server;
import lombok.SneakyThrows;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.HashMap;
import java.util.Map;

public class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,Object> {


    @SneakyThrows
    @Override
    public void completed(AsynchronousSocketChannel result, Object attachment) {
        System.out.println(result.getRemoteAddress());
        if (result.isOpen()){
            ClientHandler clientHandler = new ClientHandler(result);
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            Map<String, Object> info = new HashMap<>();
            info.put("type", "read");
            info.put("buffer", buffer);
            result.read(buffer,info,clientHandler);
        }
    }

    @Override
    public void failed(Throwable exc, Object attachment) {

    }
}
